//
//  WarmVolumeTimeViewController.m
//  LFCWarmSystem
//
//  Created by Kill on 16/4/29.
//  Copyright © 2016年 李福才. All rights reserved.
//

#import "WarmVolumeTimeViewController.h"

@interface WarmVolumeTimeViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,MFMessageComposeViewControllerDelegate>{
    UILabel *lblTime;
    UILabel *lblLevel;
    NSArray *arrVolume;
}

@end

@implementation WarmVolumeTimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self uiInit];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)uiInit{
    arrVolume = @[NSLocalizedString(@"Mute_Sample", @"静音"),NSLocalizedString(@"Low_Sample",@"低"),NSLocalizedString(@"Hight_Sample",@"高")];
    UILabel *lblHeadView = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height/5.0)];
    lblHeadView.text = NSLocalizedString(@"VolumePeal_Sample",@"警号音量和鸣响时间");
    lblHeadView.textColor = COLOR_MAINHEAD;
    lblHeadView.font = TABLEHEADFONT;
    lblHeadView.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lblHeadView];
    
    UILabel *lblVolume = [[UILabel alloc]initWithFrame:CGRectMake(0, lblHeadView.frame.size.height+lblHeadView.frame.origin.y, self.view.frame.size.width/2.0, 44*WIDTHSCALE)] ;
    lblVolume.text = NSLocalizedString(@"Volume_Sample",@"音量");
    lblVolume.textAlignment = NSTextAlignmentCenter;
    lblVolume.font = TABLEHEADFONT;
    lblVolume.textColor = WHITECOLOR;
    lblVolume.backgroundColor = TEXTFIELDCOLOR;
    [self.view addSubview:lblVolume];
    UILabel *lblTimeRight = [[UILabel alloc]initWithFrame:CGRectMake(lblVolume.frame.size.width+lblVolume.frame.origin.x, lblVolume.frame.origin.y, lblVolume.frame.size.width, lblVolume.frame.size.height)] ;
    lblTimeRight.text = NSLocalizedString(@"Time_Sample",@"时间");
    lblTimeRight.font = TABLEHEADFONT;
    lblTimeRight.textColor = WHITECOLOR;
    lblTimeRight.backgroundColor = TEXTFIELDCOLOR;
    lblTimeRight.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lblTimeRight];
    
    lblLevel = [[UILabel alloc]initWithFrame:CGRectMake(0, lblVolume.frame.size.height+lblVolume.frame.origin.y+1, lblVolume.frame.size.width,  88*WIDTHSCALE)] ;
    lblLevel.text = NSLocalizedString(@"Low_Sample",@"低");
    lblLevel.textColor = WHITECOLOR;
    lblLevel.font = TABLEHEADFONT;
    lblLevel.backgroundColor = TEXTFIELDCOLOR;
    lblLevel.textAlignment = NSTextAlignmentCenter;
    lblLevel.alpha = 0.75;
    [self.view addSubview:lblLevel];
    
    lblTime = [[UILabel alloc]initWithFrame:CGRectMake(lblLevel.frame.size.width+lblLevel.frame.origin.x+1, lblLevel.frame.origin.y, lblLevel.frame.size.width, 88*WIDTHSCALE)] ;
    lblTime.textColor = WHITECOLOR;
    lblTime.text = [NSString stringWithFormat:@"1%@",NSLocalizedString(@"Minute_Sample", @"分钟")];
    lblTime.font = TABLEHEADFONT;
    lblTime.backgroundColor = TEXTFIELDCOLOR;
    lblTime.textAlignment = NSTextAlignmentCenter;
    lblTime.alpha = 0.75;
    [self.view addSubview:lblTime];
    
    UIPickerView *pickeV = [[UIPickerView alloc] initWithFrame:CGRectMake(0, lblTime.frame.size.height+lblTime.frame.origin.y, MAINSCREENBOUNDS.size.width, MAINSCREENBOUNDS.size.height-lblTime.frame.size.height-lblTime.frame.origin.y)];
    pickeV.delegate = self;
    pickeV.dataSource = self;
    [self.view addSubview:pickeV];
}


#pragma mark-UIPickerViewDateSource
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return 3;
    }else{
        return 9;
    }
}
#pragma mark-UIPickerViewDelegate
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return self.view.frame.size.width/3.0;
}
//-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    NSString *title;
//    switch (component) {
//        case 0:{
//            title = arrVolume[row];
//        } break;
//        case 1:{
//            title = [NSString stringWithFormat:@"%ld %@",row+1,NSLocalizedString(@"Minute_Sample", @"分钟")];
//        } break;
//        default:
//            break;
//    }
//    return title;
//}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 44;
}

-(void)rightAction:(id)sender{
    NSString *msgContect = [self languageMSG:@"Siren Volume(0 Mute,1 Low,2 High):" and:@"Siren ringing(1-9min):"];
    [self showSMSPicker:msgContect];
}
- (NSString *)languageMSG:(NSString*)content1 and:(NSString *)content2{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"mulLan" ofType:@"plist"];
    NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSString *str = @"";
    if ((str = [data objectForKey:[NSString stringWithFormat:@"CAlarmVolSMS_%@ ",self.modelPhone.language]])) {
        content1 = str ;
        
    }
    NSString *str2 = @"";
    if ((str2 = [data objectForKey:[NSString stringWithFormat:@"CAlarmRingingTimeSMS_%@ ",self.modelPhone.language]])) {
        content2 = [[str2 componentsSeparatedByString:@"\\r\\n"] objectAtIndex:1];
        
    }
    
    return [NSString stringWithFormat:@"%@\n%@\n%@\n%@" ,[[content1 componentsSeparatedByString:@"\\r"] firstObject], lblLevel.text,content2,lblTime.text];
}
#pragma mark-Send Message
-(void)showSMSPicker:(NSString *)MSGContent{
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
    if (messageClass != nil) {
        // Check whether the current device is configured for sending SMS messages
        if ([messageClass canSendText]) {
            MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
            picker.recipients = [NSArray arrayWithObjects:self.modelPhone.phoneNumber, nil];
            picker.messageComposeDelegate = self;
            picker.body = MSGContent;
            [self presentViewController:picker animated:YES completion:^{
            }];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"DeviceCantMSG", @"设备没有短信功能")  message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
            [alert show];
        }
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"VersionProblem", @"iOS版本过低,iOS4.0以上才支持程序内发送短信") message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
        [alert show];
    }
}

//#pragma mark-MFMessageComposeViewControllerDelegate
//- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
//    
//    switch (result)
//    {
//        case MessageComposeResultCancelled:
//            NSLog(@"Result: SMS sending canceled");
//            break;
//        case MessageComposeResultSent:
//            NSLog(@"Result: SMS sent");
//            break;
//        case MessageComposeResultFailed:{
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"SendMSGFail", @"短信发送失败")  message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
//            [alert show];
//        }
//            break;
//        default:
//            NSLog(@"Result: SMS not sent");
//            break;
//    }
//    [self dismissViewControllerAnimated:YES completion:^{}];
//}
#pragma mark-UIPickerViewDelegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
    NSString *title;
    UILabel *lblPickTime = [[UILabel alloc]init];
    lblPickTime.textAlignment = NSTextAlignmentCenter;
    lblPickTime.font = TABLESECTIONFONT;
    switch (component) {
        case 0:{
            lblPickTime.frame = CGRectMake(0, 0, pickerView.frame.size.width/2.0, 44);
            title = arrVolume[row];
            if (1 == row) {
                lblPickTime.font = PICKVIEWCELLFONT;
                lblLevel.text = arrVolume[row];
                [pickerView selectRow:row inComponent:component animated:YES];
            }
        } break;
        case 1:{
            lblPickTime.frame = CGRectMake(pickerView.frame.size.width/2.0, 0, pickerView.frame.size.width/2.0, 44);
            title = [NSString stringWithFormat:@"%ld %@",row+1,NSLocalizedString(@"Minute_Sample", @"分钟")];
            if (4 == row) {
                lblPickTime.font = PICKVIEWCELLFONT;
                lblTime.text = [NSString stringWithFormat:@"%ld %@",row+1,NSLocalizedString(@"Minute_Sample", @"分钟")];
                [pickerView selectRow:row inComponent:component animated:YES];
            }
        } break;
        default:
            break;
    }
    lblPickTime.text = title;
    return lblPickTime;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    UILabel *lbl = (UILabel *)[pickerView viewForRow:row forComponent:component];
    lbl.font = PICKVIEWCELLFONT;
    switch (component) {
        case 0:
            lblLevel.text = arrVolume[row];
            
            break;
        case 1:
            lblTime.text = [NSString stringWithFormat:@"%ld %@",row+1,NSLocalizedString(@"Minute_Sample", @"分钟")];
            break;
            
        default:
            break;
    }
}

@end
